﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace YXShop.Web.membercenter.shop.shop_administer
{
    public partial class shop_order_update : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = ChangeHope.WebPage.PageRequest.GetQueryInt("OrderId");
            if (!IsPostBack)
            {
                this.DeliverBind();
                this.OrderInfo(id);
                this.OrdersProduct(id);
            }
        }

        #region 订单信息
        private void OrderInfo(int id)
        {
            YXShop.BLL.Order.Orders bll = new YXShop.BLL.Order.Orders();
            YXShop.Model.Order.Orders model = bll.GetModel(id);
            if (model != null)
            {
                YXShop.BLL.Member.MemberAccount mabll = new YXShop.BLL.Member.MemberAccount();
                YXShop.Model.Member.MemberAccount mamodel = mabll.GetModel(model.UserId);
                if (mamodel != null)
                {
                    YXShop.BLL.Member.MemberInfo mibll = new YXShop.BLL.Member.MemberInfo();
                    YXShop.Model.Member.MemberInfo mimodel = mibll.GetModel(mamodel.UID);
                    if (mimodel != null)
                    {
                        this.lbName.Text = mimodel.TrueName;
                    }
                    this.lbUserName.Text = "<a href='../member/member_view.aspx?uid=" + mamodel.UID + "'>" + model.UserId + "</a>";
                }
                else
                {
                    this.lbUserName.Text = model.UserId;
                }
                this.blOrderNo.Text = model.OrderId;
                YXShop.Common.OrdersStatusEnum ose = new YXShop.Common.OrdersStatusEnum();
                this.lbOrderStatue.Text = ose.OrderStatus(model.OrderStatus);
                this.lbLogisticsStatus.Text = ose.OgisticsStatu(model.OgisticsStatus);
                this.lbPayment.Text = ose.PaymentStatus(model.PaymentStatus);
                this.lbOrderDateTime.Text = model.OrderDate.ToString();
                this.txtConsigneeName.Text = model.ConsigneeName;
                this.txtConsigneeAddress.Text = model.ConsigneeAddress;
                this.txtConsigneeEmail.Text = model.ConsigneeEmail;
                this.txtConsigneeModile.Text = model.ConsigneePhone;
                this.txtConsigneeTel.Text = model.ConsigneeTel;
                this.txtConsigneeZip.Text = model.ConsigneeZip;
                this.txtRemark.Text = string.IsNullOrEmpty(model.Remark) ? "" : model.Remark;
                this.ddlPayMent.SelectedValue = model.PaymentType.ToString();
                this.ddlDeliver.SelectedValue = model.Courier.ToString();
                this.txtTotalPrice.Text = Convert.ToDouble(model.TotalPrice).ToString("f2");
                this.txtCarriage.Text = Convert.ToDouble(model.Carriage).ToString("f2");
            }
        }
        #endregion

        #region 订单商品信息
        private void OrdersProduct(int orderId)
        {
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            YXShop.BLL.Order.OrderProduct bll = new YXShop.BLL.Order.OrderProduct();
            ChangeHope.DataBase.DataByPage dataPage = bll.GetListByPage(" and OrderId=" + orderId);
            table.AddHeadCol("", "商品名称");
            table.AddHeadCol("", "单价");
            table.AddHeadCol("", "数量");
            table.AddHeadCol("", "小计(单位：元)");
            table.AddRow();
            double ProductTotalPrice = 0;
            if (dataPage.DataReader != null)
            {
                while (dataPage.DataReader.Read())
                {
                    table.AddCol(dataPage.DataReader["ProName"].ToString());
                    table.AddCol(Convert.ToDouble(dataPage.DataReader["ProPrice"].ToString()).ToString("f4"));
                    table.AddCol(dataPage.DataReader["ProNum"].ToString());
                    table.AddCol((Convert.ToDouble(dataPage.DataReader["ProPrice"].ToString()) * Convert.ToDouble(dataPage.DataReader["ProNum"].ToString())).ToString("f4"));
                    table.AddRow();
                    ProductTotalPrice += Convert.ToDouble(dataPage.DataReader["ProPrice"].ToString()) * Convert.ToDouble(dataPage.DataReader["ProNum"].ToString());
                }
                table.AddCol("4", "<strong>合计：" + ProductTotalPrice.ToString("f4") + "</strong>");
                table.AddRow();
                string view = table.GetTable() + dataPage.PageToolBar;
                dataPage.Dispose();
                dataPage = null;
                this.litData.Text = view;
            }
            else
            {
                this.litData.Text = "没有找到该订单的商品信息";
            }

        }
        #endregion

        protected void DeliverBind()
        {
            YXShop.BLL.Product.Deliver bll = new YXShop.BLL.Product.Deliver();
            List<YXShop.Model.Product.Deliver> list = bll.GetAll();
            this.ddlDeliver.DataSource = list;
            this.ddlDeliver.DataTextField = "Name";
            this.ddlDeliver.DataValueField = "ID";
            this.ddlDeliver.DataBind();
        }

        private void Save()
        {
            YXShop.BLL.Order.Orders bll = new YXShop.BLL.Order.Orders();
            YXShop.Model.Order.Orders model = bll.GetModel(this.blOrderNo.Text);
            model.ConsigneeName = this.txtConsigneeName.Text;
            model.ConsigneeAddress = this.txtConsigneeAddress.Text;
            model.ConsigneeEmail = this.txtConsigneeEmail.Text;
            model.ConsigneePhone = this.txtConsigneeModile.Text;
            model.ConsigneeTel = this.txtConsigneeTel.Text;
            model.ConsigneeZip = this.txtConsigneeZip.Text;
            model.Remark = this.txtRemark.Text.Trim();
            if (ChangeHope.Common.ValidateHelper.IsDecimal(this.txtCarriage.Text.Trim()))
            {
                model.Carriage = Convert.ToDecimal(this.txtCarriage.Text.Trim());
            }
            else
            {
                this.ltlMsg.Text = "操作失败,请输入数值，运输费不能为负数。";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }
            if (ChangeHope.Common.ValidateHelper.IsDecimal(this.txtTotalPrice.Text.Trim()))
            {
                model.TotalPrice = Convert.ToDecimal(this.txtTotalPrice.Text.Trim());
            }
            else
            {
                this.ltlMsg.Text = "操作失败,请输入数值，买家实付（不含运费）必须大于0。有取消部分交易时，请选择取消理由。";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }
            try
            {
                model.FactPrice = Convert.ToDecimal(this.txtTotalPrice.Text.Trim()) + Convert.ToDecimal(this.txtCarriage.Text.Trim());
                bll.Update(model);
                this.ltlMsg.Text = "操作成功，已保存该信息";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";
            }
            catch
            {
                this.ltlMsg.Text = "操作失败";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
            }
            finally
            {
                bll = null;
                model = null;
                GC.Collect();
            }
        }

        protected void linkOk_Click(object sender, EventArgs e)
        {
            Save();
        }
    }
}
